//
//  HistoryViewController.m
//  Double
//
//  Created by dllo on 15/5/25.
//  Copyright (c) 2015年 袁庆秋. All rights reserved.
//

#import "HistoryViewController.h"
#import "HistoryDatabase.h"
#import "HistoryModel.h"
#import "CartoonRecommendDetailViewController.h"
#import "HistoryTableViewCell.h"
#import <UIImageView+WebCache.h>
@interface HistoryViewController ()<UITableViewDataSource, UITableViewDelegate>

@end

@implementation HistoryViewController

- (void)dealloc
{
    [_tableView release];
    [_tableArray release];
    [super dealloc];
}
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.tableArray = [NSArray array];
    }
    return self;
}
- (void)loadView
{
    [super loadView];
    self.navigationItem.title = @"历史纪录";
    self.navigationController.navigationBar.translucent = NO;
    [self createView];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"fanhui.png"] style:UIBarButtonItemStylePlain target:self action:@selector(leftbuttonAction)];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
}
- (void)leftbuttonAction
{
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark 创建TableView
- (void)createView
{
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];
    self.tableView.separatorColor = [UIColor clearColor];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    [self.view addSubview:self.tableView];
    [self.tableView release];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 135;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

{
    self.tableArray = [HistoryDatabase selectAll];
    return self.tableArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{
    static NSString * cellInde = @"cell";
    HistoryTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellInde];
    if (!cell) {
        cell = [[[HistoryTableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellInde]autorelease];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    self.tableArray = [HistoryDatabase selectAll];

        
        HistoryModel *model1 = [self.tableArray objectAtIndex:indexPath.row];
        [cell.label1 setText:model1.HistoryName];
        [cell.image sd_setImageWithURL:[NSURL URLWithString:model1.HistoryPic] placeholderImage:[UIImage imageNamed:@"placeholder.jpg"] options:SDWebImageRetryFailed];
    
    return cell;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    self.tableArray = [HistoryDatabase selectAll];
    if (self.tableArray.count == 0) {
        return @"暂无历史记录";
    }
    return nil;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    CartoonRecommendDetailViewController *cartoon = [[CartoonRecommendDetailViewController alloc] init];
    self.tableArray = [HistoryDatabase selectAll];
    HistoryModel *model1 = [self.tableArray objectAtIndex:indexPath.row];
    cartoon.iD = [NSString stringWithFormat:@"%ld", model1.HistoryId];
    cartoon.titleName = model1.HistoryName;
    [self.navigationController pushViewController:cartoon animated:YES];
    [cartoon release];
}
// 重写系统按钮触发的方法(让其处于编辑状态)
-(void)setEditing:(BOOL)editing animated:(BOOL)animated
{
    [super setEditing:editing animated:animated];
    [self.tableView setEditing:editing animated:animated];
}
// 编辑删除和插入
-(UITableViewCellEditingStyle) tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == self.tableArray.count) {
        return UITableViewCellEditingStyleInsert;
    }
    else {
        return UITableViewCellEditingStyleDelete;
    }
}
#pragma mark - 主要
// 删除对应数据
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        //        [self.tableArray removeObjectAtIndex:indexPath.row];
        self.tableArray = [HistoryDatabase selectAll];
        HistoryModel *model1 = [self.tableArray objectAtIndex:indexPath.row];
        [HistoryDatabase deleteWithModel:model1];
    }
    [self.tableView reloadData];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
